package com.wwwarehouse.taskcenter.fragment.invite_business_information;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.widget.AdapterView;
import android.widget.GridView;

import com.alibaba.fastjson.JSON;
import com.wwwarehouse.common.activity.base.BaseFragment;
import com.wwwarehouse.common.bean.response.CommonClass;
import com.wwwarehouse.common.custom_widget.state_layout.StateLayout;
import com.wwwarehouse.common.http.NoHttpUtils;
import com.wwwarehouse.common.tools.LogUtils;
import com.wwwarehouse.taskcenter.R;
import com.wwwarehouse.taskcenter.adapter.SelectInviterChildAdapter;
import com.wwwarehouse.taskcenter.bean.InviteRankFilterBean;
import com.wwwarehouse.taskcenter.bean.InviteRankOrderBean;
import com.wwwarehouse.taskcenter.bean.InviterRankBean;
import com.wwwarehouse.taskcenter.constant.TaskCenterConstant;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @创建者 chenchao.
 * @创建时间 2017/10/9 19:43
 * @描述 ${TODO}
 */

public class SelectInviterFragmentChild extends BaseFragment {

    private View                           mView;
    private StateLayout                    mStateLayout;
    private GridView                       mGridView;
    private List<InviteRankFilterBean>     mInviteRankFilterList;
    private Map                            mInviteRankingMap;
    private List<InviteRankOrderBean>      mOrderList;
    private InviteRankOrderBean            mInviteRankOrderBean;
    private List<String>                   mSelectsList;
    private Bundle                         mSelectInviteBundle;
    private InviteRankFilterBean           mMInviteRankFilterBean1;
    private InviteRankFilterBean           mMInviteRankFilterBean2;
    private InviteRankFilterBean           mMInviteRankFilterBean3;
    private InviteRankFilterBean           mMInviteRankFilterBean4;
    private InviteRankFilterBean           mMInviteRankFilterBean5;
    private InviteRankFilterBean           mMInviteRankFilterBean6;
    private InviteRankFilterBean           mInviteRankFilterBean7;
    private boolean                        isDay;
    private boolean                        isWeek;
    private boolean                        isMonth;
    private boolean                        isYear;
    private List<InviterRankBean.ListBean> mSelectInviteChildList;
    private int                            mPage;
    private String mStartTime;
    private String mEndTime;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mView = inflater.inflate(R.layout.fragment_select_inviter_child, container, false);
        return mView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
        initData();
    }

    private void initView() {
        mStateLayout = findView(mView, R.id.sl_state);
        mGridView = findView(mView, R.id.gv_resourse);

    }

    private void initData() {
        mInviteRankFilterList = new ArrayList<>();//筛选条件的集合
        mInviteRankingMap = new HashMap();//请求数据的map

        getSelectsList();//最后一个集合

        mSelectInviteBundle = getArguments();
        if (mSelectInviteBundle != null && mSelectInviteBundle.getString("startTime") != null && mSelectInviteBundle.getString("endTime") != null) {
            mStartTime = mSelectInviteBundle.getString("startTime");
            mEndTime = mSelectInviteBundle.getString("endTime");
        }
        //day
        if (mSelectInviteBundle != null && mSelectInviteBundle.getSerializable("mInviteRankFilterBean1") != null && mSelectInviteBundle.getSerializable("mInviteRankFilterBean2") != null) {
            isDay = true;
            mPage = mSelectInviteBundle.getInt("page");
            mMInviteRankFilterBean1 = (InviteRankFilterBean) mSelectInviteBundle.getSerializable("mInviteRankFilterBean1");
            mMInviteRankFilterBean2 = (InviteRankFilterBean) mSelectInviteBundle.getSerializable("mInviteRankFilterBean2");
            mInviteRankOrderBean = (InviteRankOrderBean) mSelectInviteBundle.getSerializable("mInviteRankOrderBean");
            getInviteRankOrderBean("asc");//升降序
            mInviteRankFilterList.clear();
            if (mSelectInviteBundle.getSerializable("mInviteRankFilterBean7") != null) {
                mInviteRankFilterBean7 = (InviteRankFilterBean) mSelectInviteBundle.getSerializable("mInviteRankFilterBean7");
                mInviteRankFilterList.add(mInviteRankFilterBean7);
            }
            mInviteRankFilterList.add(mMInviteRankFilterBean1);
            mInviteRankFilterList.add(mMInviteRankFilterBean2);
            mInviteRankingMap.put("dataSet", "inviter_day");

            //week
        } else {
            if (mSelectInviteBundle != null && mSelectInviteBundle.getSerializable("mInviteRankFilterBean1") != null && mSelectInviteBundle.getSerializable("mInviteRankFilterBean3") != null &&
                    mSelectInviteBundle.getSerializable("mInviteRankFilterBean4") != null) {
                isWeek = true;
                mPage = mSelectInviteBundle.getInt("page");
                mMInviteRankFilterBean1 = (InviteRankFilterBean) mSelectInviteBundle.getSerializable("mInviteRankFilterBean1");
                mMInviteRankFilterBean3 = (InviteRankFilterBean) mSelectInviteBundle.getSerializable("mInviteRankFilterBean3");
                mMInviteRankFilterBean4 = (InviteRankFilterBean) mSelectInviteBundle.getSerializable("mInviteRankFilterBean4");
                mInviteRankOrderBean = (InviteRankOrderBean) mSelectInviteBundle.getSerializable("mInviteRankOrderBean");
                getInviteRankOrderBean("asc");//升降序
                //清一遍再添加 以保证每次请求都可以用这个集合
                mInviteRankFilterList.clear();
                if (mSelectInviteBundle.getSerializable("mInviteRankFilterBean7") != null) {
                    mInviteRankFilterBean7 = (InviteRankFilterBean) mSelectInviteBundle.getSerializable("mInviteRankFilterBean7");
                    mInviteRankFilterList.add(mInviteRankFilterBean7);
                }
                mInviteRankFilterList.add(mMInviteRankFilterBean1);//暂时不传 石生未写好
                mInviteRankFilterList.add(mMInviteRankFilterBean3);
                mInviteRankFilterList.add(mMInviteRankFilterBean4);

                mInviteRankingMap.put("dataSet", "inviter_week");
                //month
            } else if (mSelectInviteBundle != null && mSelectInviteBundle.getSerializable("mInviteRankFilterBean1") != null && mSelectInviteBundle.getSerializable("mInviteRankFilterBean5") != null
                    ) {
                isMonth = true;
                mPage = mSelectInviteBundle.getInt("page");
                mMInviteRankFilterBean1 = (InviteRankFilterBean) mSelectInviteBundle.getSerializable("mInviteRankFilterBean1");
                mMInviteRankFilterBean5 = (InviteRankFilterBean) mSelectInviteBundle.getSerializable("mInviteRankFilterBean5");
                mInviteRankOrderBean = (InviteRankOrderBean) mSelectInviteBundle.getSerializable("mInviteRankOrderBean");
                getInviteRankOrderBean("asc");//升降序
                mInviteRankFilterList.clear();
                if (mSelectInviteBundle.getSerializable("mInviteRankFilterBean7") != null) {
                    mInviteRankFilterBean7 = (InviteRankFilterBean) mSelectInviteBundle.getSerializable("mInviteRankFilterBean7");
                    mInviteRankFilterList.add(mInviteRankFilterBean7);
                }
                mInviteRankFilterList.add(mMInviteRankFilterBean1);
                mInviteRankFilterList.add(mMInviteRankFilterBean5);
                mInviteRankingMap.put("dataSet", "inviter_month");
                //year
            } else if (mSelectInviteBundle != null && mSelectInviteBundle.getSerializable("mInviteRankFilterBean1") != null && mSelectInviteBundle.getSerializable("mInviteRankFilterBean6") != null
                    ) {
                isYear = true;
                mPage = mSelectInviteBundle.getInt("page");
                mMInviteRankFilterBean1 = (InviteRankFilterBean) mSelectInviteBundle.getSerializable("mInviteRankFilterBean1");
                mMInviteRankFilterBean6 = (InviteRankFilterBean) mSelectInviteBundle.getSerializable("mInviteRankFilterBean6");
                mInviteRankOrderBean = (InviteRankOrderBean) mSelectInviteBundle.getSerializable("mInviteRankOrderBean");
                getInviteRankOrderBean("asc");//升降序
                mInviteRankFilterList.clear();
                if (mSelectInviteBundle.getSerializable("mInviteRankFilterBean7") != null) {
                    mInviteRankFilterBean7 = (InviteRankFilterBean) mSelectInviteBundle.getSerializable("mInviteRankFilterBean7");
                    mInviteRankFilterList.add(mInviteRankFilterBean7);
                }
                mInviteRankFilterList.add(mMInviteRankFilterBean1);
                mInviteRankFilterList.add(mMInviteRankFilterBean6);
                mInviteRankingMap.put("dataSet", "inviter_year");
            }
        }
        getSelectInviterChild();

    }

    private void getSelectInviterChild() {
        mInviteRankingMap.put("filters", mInviteRankFilterList);
        mInviteRankingMap.put("ifCount", "true");
        mInviteRankingMap.put("ifDistinct", "true");
        mInviteRankingMap.put("page", mPage);
        mInviteRankingMap.put("size", 9);
        mInviteRankingMap.put("orders", mOrderList);
        mInviteRankingMap.put("selects", mSelectsList);
        mStateLayout.showProgressView(false);
        NoHttpUtils.httpPost(TaskCenterConstant.BI_GETDATA, mInviteRankingMap, mOnResponseListener, 0);
    }

    private NoHttpUtils.OnResponseListener mOnResponseListener = new NoHttpUtils.OnResponseListener() {
        @Override
        public void onSucceed(CommonClass commonClass, int i) {
            try {
                switch (i) {
                    case 0:
                        if ("0".equals(commonClass.getCode())) {
                            InviterRankBean inviterRankBean = JSON.parseObject(commonClass.getData().toString(), InviterRankBean.class);
                            mSelectInviteChildList = inviterRankBean.getList();
                            SelectInviterChildAdapter selectInviterChildAdapter = new SelectInviterChildAdapter(mActivity, mSelectInviteChildList, mGridView);
                            mGridView.setAdapter(selectInviterChildAdapter);


                            mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                                @Override
                                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                                    Bundle bundle = new Bundle();
                                    bundle.putInt("inviterId", mSelectInviteChildList.get(position).getInviterId());
                                    bundle.putString("InviterName", mSelectInviteChildList.get(position).getInviterName());
                                    bundle.putString("startTime", mStartTime);
                                    bundle.putString("endTime", mEndTime);
                                    if (isDay) {
                                        bundle.putSerializable("mInviteRankFilterBean1", mMInviteRankFilterBean1);
                                        bundle.putSerializable("mInviteRankFilterBean2", mMInviteRankFilterBean2);
                                    } else if (isWeek) {
                                        bundle.putSerializable("mInviteRankFilterBean1", mMInviteRankFilterBean1);
                                        bundle.putSerializable("mInviteRankFilterBean3", mMInviteRankFilterBean3);
                                        bundle.putSerializable("mInviteRankFilterBean4", mMInviteRankFilterBean4);
                                    } else if (isMonth) {
                                        bundle.putSerializable("mInviteRankFilterBean1", mMInviteRankFilterBean1);
                                        bundle.putSerializable("mInviteRankFilterBean5", mMInviteRankFilterBean5);
                                    } else if (isYear) {
                                        bundle.putSerializable("mInviteRankFilterBean1", mMInviteRankFilterBean1);
                                        bundle.putSerializable("mInviteRankFilterBean6", mMInviteRankFilterBean6);
                                    }
                                    pushFragment(TaskCenterConstant.InviteSuccessFragment, bundle);
                                }
                            });
                        } else {
                            mStateLayout.showSystemView(commonClass.getMsg(), false);
                            mStateLayout.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    getSelectInviterChild();
                                }
                            });
                        }
                        break;
                }
            } catch (Exception e) {
                LogUtils.showLog(e.toString());
            }
        }

        @Override
        public void onFailed(String s, int i) {
            mStateLayout.showNetworkView(false);
            mStateLayout.setNetworkListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    getSelectInviterChild();
                }
            });
        }

        @Override
        public void onFinish(int i) {
            mStateLayout.showContentView();
        }
    };

    private void getInviteRankOrderBean(String s) {
        mOrderList = new ArrayList<>();
        //        mInviteRankOrderBean = new InviteRankOrderBean();
        //        mInviteRankOrderBean.setColumn("invite_ranking");
        //        mInviteRankOrderBean.setType(s);//升序
        mOrderList.add(mInviteRankOrderBean);
    }

    private void getSelectsList() {
        mSelectsList = new ArrayList<>();
        mSelectsList.add("inviter_id");
        mSelectsList.add("inviter_name");
        mSelectsList.add("register_qty");
        mSelectsList.add("inviter_url");
        mSelectsList.add("invite_ranking");
    }

    @Override
    public void setTitle() {

    }

    @Override
    public void requestDatas() {

    }

    @Override
    public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) {
        return null;
    }
}


